<?php
require_once('SSUtil.php');
/*
 * Created on May 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
function extractURL($text) {
   	//preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$text,$matches);
  
   	if(preg_match('@(https?://[^ ]*)@',$text,$matches)) {
   		$url = $matches[0];
	}
	else $url='';
   	return $url;
}
function createEmboodle($description) {
	$url=extractURL($description);
	
	$url_parts = parse_url($url);
	if(!isset($url_parts['host']))
		return;
	$domain=$url_parts['host'];

    if($domain=='www.youtube.com'){
    	preg_match(
        	'/[\\?\\&]v=([^\\?\\& ]+)/',
        	$url,
        	$matches);
   
   		$id=$matches[1];	 	
		//echo $matches[1];  
		$embedcode =  '<object width="560" height="340">'.
						'<param name="movie" value="http://www.youtube.com/v/'.$id.'=en&fs=1&">' .
						'</param><param name="allowFullScreen" value="true">' .
						'</param><param name="allowscriptaccess" value="always">' .
						'</param>' .
						'<embed src="http://www.youtube.com/v/'.$id.'&hl=en&fs=1&"' .
								'type="application/x-shockwave-flash"' .
								'allowscriptaccess="always" ' .
								'allowfullscreen="true"' .
								'width="560"' .
								'height="340">' .
						'</embed>' .
						'</object>';
		$query="select * from html where url = \"".$url. "\" and xpath=\"//title\"";
    	$request= "http://query.yahooapis.com/v1/public/yql?q=".urlencode($query);
        $yql_response=file_get_contents($request);
        $xml = simplexml_load_string($yql_response);
        $result=$xml->xpath('//results/title');
        if($result!=false) {
        	$title=(string)$result[0];
        	$status = 'success';
        }
         else {
         	 $title='';
         	 $status = 'failure';
         }
         
		//$title='youtube stuff';
		$status = 'success';
    }
    
    else if($domain=='www.slideshare.net') {
    	$apiobj = new SSUtil();
 		$slideObj=$apiobj->get_slideInfoByUrl($url);
 		//if slideojb exists...TODO FIXME
 		if($slideObj['Error']==true) {
 			$embedcode = $slideObj['Message'];
 			$status='failure';
 		//	print_r($slideObj);
	    }
	    else {
	    	
	    	$cleaned_string=str_replace("&", "&amp;", $slideObj['EMBED']);
	    	// this is pretty kludgy, but simplexml balks at &s that aren't
	    	//part of a HTML character thinger
	 		$xml = simplexml_load_string($cleaned_string);
 			$object_xml = $xml->xpath("//object");
 			$embedcode=$object_xml[0]->asXml();
 			$title= $slideObj['TITLE'];
 			$status = 'success';
	    }
    }
    
    else if($domain=='www.flickr.com'){
    	$query="select src,alt from html where url = \"".$url. "\" and xpath=\"//div[@class='photoImgDiv']/img\"";
    	$request= "http://query.yahooapis.com/v1/public/yql?q=".urlencode($query);
    	
    	/*$ch = curl_init($request);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        //echo curl_error();
        //var_dump($output); 
        //print_r( $output);     
        curl_close($ch);*/

        $yql_response=file_get_contents($request);
        $xml = simplexml_load_string($yql_response);
        $result=$xml->xpath('//results/img');
        if($result!=false) {
        	$embedcode=$result[0]->asXml();
        	$status = 'success';
        }
         else {
         	 $embedcode='';
         	 $status = 'failure';
         }
       	
       	$query="select * from html where url = \"".$url. "\" and xpath=\"//title\"";
    	$request= "http://query.yahooapis.com/v1/public/yql?q=".urlencode($query);
        $yql_response=file_get_contents($request);
        $xml = simplexml_load_string($yql_response);
        $result=$xml->xpath('//results/title');
        if($result!=false) {
        	$title=(string)$result[0];
        }
         else {
         	 $title='';   
         }
    }
    
    //else if(url!='') {
    //	$embedcode =  "<a href='".$url."'> A link</a> ";
    //}
    else {
    	$embedcode = 'failure';
    	$status='unknown_domain';
    }
    
    $array = array('embedcode' => $embedcode, 'title'=>$title, 'url' => $url,'status' => $status);
	return $array;  
    
}
?>

